package com.eclipse.org.snms.summary;

import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class ConcreteSummaryViewer extends ViewPart {
	
	
	public static final String ID = "SummaryPlugin.view";
	//Current working directory
 	private static String cwd = System.getProperty("user.dir");
 	
	private Table table;
	private TableViewer tableViewer;
	private Button deleteButton, addButton, viewButton, modifyButton;
	private ExampleTaskList taskList = new ExampleTaskList();
	
	// Set the table column property names
	private final String PRIORITY_COLUMN 	= "Priority";
	private final String DATE_COLUMN 		= "Date";
	private final String TITLE_COLUMN		= "Title";
	
	private String[] columnNames = new String[] { 
			DATE_COLUMN,
			PRIORITY_COLUMN,
			TITLE_COLUMN 
			};
	private List<String> entries;

	private static final Object[] CONTENT = new Object[] {
	      new EditableTableItem("item 1", new Integer(0)),
	      new EditableTableItem("item 2", new Integer(1)) 
	      };
	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements IStructuredContentProvider {
	
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object inputElement) {
			//ExampleTask example[] = new ExampleTask[2];// = new ExampleTask[10];
			//example[0] = new ExampleTask("my first task");
			//example[1] = new ExampleTask("my second task");// (ExampleTask[]) taskList.getTasks().toArray();
			//ExampleTask test = (ExampleTask) taskList.getTasks().get(0);
			//System.out.print(test.getDescription());			 
			//return (Object[]) inputElement;
			//return new String[] {"1","03/30/2009","Submit 2nd deliverable."};//taskList.getTasks().toArray();
			return ((List) inputElement).toArray();
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		addChildControls(parent);
	}
	
	/**
	 * Create a new shell, add the widgets, open the shell
	 * @return the shell that was created	 
	 */
	private void addChildControls(Composite composite) {
		
		// Set layout 
		GridLayout layout = new GridLayout(1, false);
		composite.getShell().setBounds(100, 100, 380, 520);
		composite.setLayout(layout);
		
		// Create a composite to hold the children
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_BOTH);
		composite.setBounds(100, 100, 400, 385);
		composite.setLayoutData(gridData);
		
		// Create and setup the TableViewer
		//createTableViewer();
		tableViewer = new TableViewer(composite);
		tableViewer.setColumnProperties(columnNames);
		
		tableViewer.setContentProvider(new ViewContentProvider());
		tableViewer.setLabelProvider(new ViewLabelProvider());
		tableViewer.addDoubleClickListener(new IDoubleClickListener(){
			public void doubleClick(DoubleClickEvent event) {
					System.out.print("double clicked. ");
			}
		});

		entries = new LinkedList<String>();
		entries.add("1-One");
	    entries.add("2-Two");
	    entries.add("3-Three");
	    tableViewer.setInput(entries);

		
		// Create the table 
		//createTable(composite);
		table = tableViewer.getTable(); 
		gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		gridData.grabExcessVerticalSpace = true;
		table.setLayoutData(gridData);		
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		//Add columns
		new TableColumn(table, SWT.LEFT, 0).setText(PRIORITY_COLUMN);
		new TableColumn(table, SWT.LEFT, 1).setText(DATE_COLUMN);
		new TableColumn(table, SWT.LEFT, 2).setText(TITLE_COLUMN);
		
		// Add the buttons
		createButtons(composite);
		refresh();
	}
	  private void refresh() {
		    tableViewer.refresh();
		    Table table = tableViewer.getTable();
		    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
		      table.getColumn(i).pack();
		    }
		    //System.out.println(table.getColumnCount());
		    table.getColumn(0).setWidth(40);
		    table.getColumn(1).setWidth(100);
		    table.getColumn(2).setWidth(200);
		  }

	/**
	 * Create the Table
	 */
	private void createTable(Composite parent) {
		int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | 
					SWT.FULL_SELECTION | SWT.HIDE_SELECTION;

		table = new Table(parent, style);
		
		
		//TableItem ti = new TableItem(table,0);
		GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		gridData.grabExcessVerticalSpace = true;
		table.setLayoutData(gridData);		
					
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		//Columns
		// 1st column 
		TableColumn column = new TableColumn(table, SWT.LEFT, 0);		
		column.setText(PRIORITY_COLUMN);
		column.setWidth(40);
		
		//2nd column
		column = new TableColumn(table, SWT.LEFT, 1);
		column.setText(DATE_COLUMN);
		column.setWidth(100);
		column.setData("String");

		// 3rd column with task Description
		column = new TableColumn(table, SWT.LEFT, 2);
		column.setText(TITLE_COLUMN);
		column.setWidth(200);
		
		// Add listener to column so tasks are sorted by description when clicked 
		column.addSelectionListener(new SelectionAdapter() {
       	
			public void widgetSelected(SelectionEvent e) {
				//tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.DESCRIPTION));
			}
		});

		// Add listener to column so tasks are sorted by owner when clicked
		column.addSelectionListener(new SelectionAdapter() {
       	
			public void widgetSelected(SelectionEvent e) {
				//tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.OWNER));
			}
		});
	}

	/**
	 * Create the TableViewer 
	 */
	private void createTableViewer() {

		tableViewer = new TableViewer(table);
		
		tableViewer.setUseHashlookup(true);
		tableViewer.setColumnProperties(columnNames);

		// Create the cell editors
		CellEditor[] editors = new CellEditor[columnNames.length];

		// Column 1 : Priority 
		TextCellEditor textEditor = new TextCellEditor(table);
		((Text) textEditor.getControl()).setTextLimit(60);
		editors[0] = textEditor;

		// Column 2 : Date (Text with digits only)
		textEditor = new TextCellEditor(table);

		editors[1] = textEditor;
		// Column 3 : Title 
		editors[2] = new TextCellEditor(table);
		 
	}

	/**
	 * Add the "Add", "Modify", "Delete" and "Close" buttons
	 * @param parent the parent composite
	 */
	private void createButtons(final Composite parent) {
		
		Group bGroup = new Group(parent,SWT.NONE);
		bGroup.setBounds(365, 0, 450, 50);
		GridLayout gl = new GridLayout(2,false);
		gl.horizontalSpacing = 100;
		bGroup.setLayout(gl);
		bGroup.setText("Actions");
		
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.grabExcessHorizontalSpace = false;
		bGroup.setLayoutData(gridData);
		
		viewButton = new Button(bGroup, SWT.PUSH | SWT.CENTER);
		//Image buttonImage = new Image((Device)parent.getDisplay(), "C:\\Users\\myassine\\workspace\\SummaryPlugin\\icons\\view.gif");
		//viewButton.setImage(buttonImage);
		viewButton.setText("View");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.widthHint = 80; 
		viewButton.setLayoutData(gridData);
		 
		viewButton.addListener(SWT.Selection, new Listener() {
		      public void handleEvent(Event event) {
				  ViewStickyNoteForm vForm = new ViewStickyNoteForm();
					vForm.open();
		      }
		    });		
	
		
		// Create and configure the "Add" button
		//buttonImage = new Image((Device)parent.getDisplay(), "C:\\Users\\myassine\\workspace\\SummaryPlugin\\icons\\new.gif");
		addButton = new Button(bGroup, SWT.PUSH | SWT.CENTER);
		addButton.setText("Add");
		//addButton.setImage(buttonImage);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.widthHint = 80;
		addButton.setLayoutData(gridData);
		
		addButton.addSelectionListener(new SelectionAdapter() {
       	
       		// Add a task to the ExampleTaskList and refresh the view
			public void widgetSelected(SelectionEvent e) {
				//taskList.addTask();
			}
		});
	
		modifyButton = new Button(bGroup, SWT.PUSH | SWT.CENTER);
		//buttonImage = new Image((Device)parent.getDisplay(), "C:\\Users\\myassine\\workspace\\SummaryPlugin\\icons\\modify.gif");
		//modifyButton.setImage(buttonImage);
		modifyButton.setText("Modify");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.widthHint = 80; 
		modifyButton.setLayoutData(gridData); 
		modifyButton.addListener(SWT.Selection, new Listener() {
		      public void handleEvent(Event event) {
				  ViewStickyNoteForm vForm = new ViewStickyNoteForm();
					vForm.open();
		      }
		    });
		
		//	Create and configure the "Delete" button
		//buttonImage = new Image((Device)parent.getDisplay(), "C:\\Users\\myassine\\workspace\\SummaryPlugin\\icons\\delete.gif");
		deleteButton = new Button(bGroup, 0);
		deleteButton.setText("Delete");
		//deleteButton.setImage(buttonImage);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.widthHint = 80; 
		deleteButton.setLayoutData(gridData); 
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		tableViewer.getControl().setFocus();
	}


	/**
	 * Return the column names in a collection
	 * 
	 * @return List  containing column names
	 */
	public java.util.List getColumnNames() {
		return Arrays.asList(columnNames);
	}

	public String[] getChoices(String property) {
		if (TITLE_COLUMN.equals(property))
			return taskList.getTitles();  // The ExampleTaskList knows about the choice of owners
		else
			return new String[]{};
	}

	public ExampleTaskList getTaskList() {
		return taskList;
	}
	
}

class EditableTableItem {
	  public String name;

	  public Integer value;

	  public EditableTableItem(String n, Integer v) {
	    name = n;
	    value = v;
	  }
	}